package com.apts.model;

import java.util.List;
import java.util.Map;

import com.apts.jfinalbase.BaseModel;

public class TbProductType extends BaseModel<TbProductType> {
	private static final long serialVersionUID = -5310102820916603178L;

	public static final TbProductType dao = new TbProductType();

	// 查找所有品种信息，已list形式返回
	public List<TbProductType> findAll() {
//		String sql = "SELECT * FROM tb_product_type limit '" + start + "'" + "," + "'" + end + "' ";
		String sql1 = "SELECT * FROM tb_product_type order by id desc ";
		List<TbProductType> list = TbProductType.dao.find(sql1);
		return list;
	}

	public TbProductType findById(String typecode) {
		System.out.println("TbProductType 里面    typecode:" + typecode);
		String sql = "SELECT * FROM tb_product_type WHERE typecode= ?";
		TbProductType record = TbProductType.dao.findFirst(sql, typecode);
		return record;

	}

	public List<TbProductType> findbytypename(String typename) {
		String sql = "SELECT * FROM tb_product_type where typename like '%" + typename + "%' ";
		List<TbProductType> list = TbProductType.dao.find(sql);
		return list;
	}

	public List<TbProductType> findbyenterprise(String epname, int start, int end) {
		String sql = "SELECT * FROM tb_product_type where typecode in(select typecode from pe where epname like '%"
				+ epname + "%' ) limit ?,? ";
		List<TbProductType> list = TbProductType.dao.find(sql, start, end);
		return list;
	}

	public void insert(Map<String, Object> map) {
		TbProductType tbproducttype = new TbProductType();
		tbproducttype.set("typecode", map.get("typecode")).set("typename", map.get("typename"))
				.set("profileimg", map.get("profileimg")).set("typeinfo", map.get("typeinfo")).save();

		System.out.println("完成插入");
	}

	public void update(Map<String, Object> map) {
//		TbProductType tbproducttype = new TbProductType().findById((String) map.get("typecode"));
		TbProductType tbproducttype = TbProductType.dao.findById( (String) map.get("id") );
		String typecode = (String) map.get("typecode");
		String typename = (String) map.get("typename");
		String profileimg = (String) map.get("profileimg");
		String typeinfo = (String) map.get("typeinfo");
		System.out.println("typecode:" + typecode + "\n");
		System.out.println("typename:" + typename + "\n");
		System.out.println("profileimg:" + profileimg + "\n");
		System.out.println("typeinfo:" + typeinfo + "\n");
		if (typename.equals("")) {
			System.out.println("typename:为空\n");
			typename = tbproducttype.getStr("typename");
		}
		if (profileimg.equals("")) {
			System.out.println("profileimg:为空\n");
			profileimg = tbproducttype.getStr("profileimg");
		}
		if (typeinfo.equals("")) {
			System.out.println("typeinfo:为空\n");
			typeinfo = tbproducttype.getStr("typeinfo");
		}

		tbproducttype.set("typename", typename).set("profileimg", profileimg).set("typeinfo", typeinfo).update();

		System.out.println("完成更新");
	}

	public void Delete(String typecode) {
		TbProductType tbproducttype = new TbProductType();
		tbproducttype.findById(typecode).delete();

	}

}
